package com.bms.controller;

import com.alibaba.fastjson.JSON;
import com.bms.Dao.UserDao;
import com.bms.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class getInfo {
    @Autowired
    UserDao userDao;

    @RequestMapping(value = "/info", params = {"token"})
    public String getUserInfo(@RequestParam String token){
        int id = Integer.parseInt(token);
        User user = null;
        user = userDao.getUserById(id);
        HashMap<String, Object> map = new HashMap<>();
        int statusCode = 50008;
        HashMap<String, Object> data = new HashMap<>();
        if(user!=null){
            statusCode = 20000;
            List<String> list = new ArrayList<>();
            list.add(user.getRoles());
            data.put("roles",list);
        }
        map.put("code",statusCode);
        map.put("data",data);
        String response = JSON.toJSONString(map);
        System.out.println("token查询结果"+user);
        return response;
    }

//    @RequestMapping("/readerInfo")
//    public String getReaderInfo(@RequestBody User user)
//    {
//         User us = userDao.getUserById(user.getId());
//         return  JSON.toJSONString(us);
//    }
    @RequestMapping("/readerInfo")
    public String getReaderInfo(@RequestHeader("X-Token") String id ){
        User us = userDao.getUserById(Integer.parseInt(id));
        return  JSON.toJSONString(us);
    }
}
